/* 
*  Copyright 2006-2008 Dynamic Site Solutions.
*  Free use of this script is permitted for commercial and non-commercial 
*  applications, subject to the requirement that this comment block be kept 
*  and not be altered.  The data and executable parts of the script may be 
*  changed as needed.  Dynamic Site Solutions makes no warranty regarding 
*  fitness of use or correct function of the script.  If you would like help
*  customizing this script or if you have other questions, contact 
*  "contact_us@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Last Updated: 2008-03-26
*/
function hasClassName(el,c){
  c=c.replace(/\-/g,'\\-');
  return (new RegExp('(^|\\s)'+c+'(\\s|$)')).test(el.className);
}
function addClassName(el,c){
  if(hasClassName(el,c)) return;
  var curClass=el.className||'';
  el.className=curClass+((curClass.length>0)?' ':'')+c;
}
function removeClassName(el,c){
  var re=new RegExp('\\s*'+c.replace(/\-/g,'\\-')+'\\s*');
  el.className=el.className.replace(re,' ').replace(/^\s*|\s*$/g,'');
}
function replaceClassName(el,oldC,newC){
  removeClassName(el,oldC),addClassName(el,newC);
}

var isMSIE=/*@cc_on!@*/false; // http://dean.edwards.name/weblog/2007/03/sniff/
/*@cc_on @if(@_jscript && !(@_win32 || @_win64) && (@_jscript_version < 5.5))
var isIEmac=true; @else @*/ var isIEmac=false; /*@end @*/
var isWebkit=(navigator.userAgent.search(/Webkit\/(\d+)/i)!=-1);
var isWebkit500plus=false;
if(isWebkit && (Number(RegExp.$1)>=500)) isWebkit500plus=true;

var ReplaceCheckboxes={
  images:[
    'http://www.dynamicsitesolutions.com/javascript/checkbox_unchecked.gif',
    'http://www.dynamicsitesolutions.com/javascript/checkbox_checked.gif'
  ],
  init:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    if(window.opera && !opera.version) return; // if older Opera version
    if(isWebkit && !isWebkit500plus) return;
    // Safari 1.0-2.x don't support checking a checkbox by clicking on its 
    // label. It seems to be fixed in 3.x.

    // we're making sure images are enabled
    var d=document, b=d.getElementsByTagName('body')[0];
    var img = b.appendChild(d.createElement('img'));
    if(!window.opera) img.className='offLeft';
    else img.setAttribute('style','position:absolute;bottom:0;right:0;');
    img.src=ReplaceCheckboxes.images[1]; // this can be any image
    img.onload=function(){
      ReplaceCheckboxes.realInit();
      img.parentNode.removeChild(img);
    }
    img.src=ReplaceCheckboxes.images[1]; // setting src a 2nd time avoids bugs
  },
  realInit:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    ReplaceCheckboxes.pairLabels();
    var inputs=document.getElementsByTagName('input'),cb,i=0,lbl;
    while(cb=inputs[i++]){
      if((cb.type.toLowerCase()!='checkbox')||!hasClassName(cb,'replace'))
        continue;
      if(!cb._lbl) continue;
      addClassName(cb,'offLeft');
      addClassName(cb._lbl,(cb.checked?'checkbox':'uncheckbox'));
      cb._lbl.onclick=cb.onclick=ReplaceCheckboxes.click;
      cb.onfocus=ReplaceCheckboxes.focus;
      cb.onblur=ReplaceCheckboxes.blur;
    }
  },
  pairLabels:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    var labels=document.getElementsByTagName('label'),lbl,i=0,fld,k,idCnt=0;
    var re=/input|textarea|select/i;
    while(lbl=labels[i++]){
      fld=null,k=0;
      if(lbl.htmlFor){
        fld=document.getElementById(lbl.htmlFor);
      } else {
        var kids=(isMSIE&&lbl.all)?lbl.all:lbl.getElementsByTagName('*');
        while(fld=kids[k++]) if(re.test(fld.nodeName)) break;
        if(fld && fld.id){       // mainly for IE5-6, which don't support 
          lbl.htmlFor = fld.id;  // implicit association of a label with its 
        } else if(fld) {         // form control, so need it done explicitly
          while(document.getElementById('zfc'+idCnt)) idCnt++;
          lbl.htmlFor = fld.id = 'zfc'+idCnt;
          idCnt++;
        }
      }
      if(!fld) continue;
      lbl._fc=fld,fld._lbl=lbl;
    }
  },
  click:function(){
    var lbl=this._lbl||this;
    if((this.nodeName.toLowerCase()=='input'?this:this._fc).checked)
      replaceClassName(lbl,'uncheckbox','checkbox');
    else
      replaceClassName(lbl,'checkbox','uncheckbox');
  },
  focus:function(){addClassName(this._lbl,'cbFocus');},
  blur:function(){removeClassName(this._lbl,'cbFocus');}
};

addEvent(window,'domload',ReplaceCheckboxes.init);